package com.night.icm.controller.base;

import com.night.icm.common.utils.PageDataDTO;
import com.night.icm.common.utils.SimpleDataDTO;
import com.night.icm.model.base.CourseDTO;
import com.night.icm.service.base.service.CourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import java.util.List;

/**
 * CourseController
 *
 * @author Liao ZhiYong
 * @date 2020/2/4
 */
@Api(tags = {"课程管理"})
@RestController
@RequestMapping("/api/icm-course")
public class CourseController {

    private final CourseService courseService;

    @Autowired
    public CourseController(CourseService courseService) {
        this.courseService = courseService;
    }

    @ApiOperation(value = "分页查询课程")
    @GetMapping(value = "")
    public PageDataDTO<CourseDTO> findOnePage(
            @ApiParam(value = "页号，从0开始", required = true, defaultValue = "0") @RequestParam("page") @Min(0) int page,
            @ApiParam(value = "每页记录条数", required = true, defaultValue = "30") @RequestParam("size") @Min(1) @Max(100) int size,
            @ApiParam(value = "排序字段，例如：字段1,asc,字段2,desc") @RequestParam(value = "sort", required = false, defaultValue = "id,desc") String sort,
            @ApiParam(value = "课程编号") @RequestParam(value = "code", required = false) String code,
            @ApiParam(value = "课程名称") @RequestParam(value = "name", required = false) String name,
            @ApiParam(value = "课程类型") @RequestParam(value = "courseTypeId", required = false) String courseTypeId,
            @ApiParam(value = "考核方式") @RequestParam(value = "evaluationMode", required = false) String evaluationMode
    ) {
        return this.courseService.findOnePage(page, size, sort, code, name, courseTypeId, evaluationMode);
    }

    @ApiOperation(value = "新增课程")
    @PostMapping(value = "")
    public CourseDTO create(
            @ApiParam(value = "课程类型信息", required = true) @RequestBody @Validated CourseDTO courseDTO
    ) {
        return this.courseService.create(courseDTO);
    }

    @ApiOperation(value = "删除课程")
    @DeleteMapping(value = "/{id}")
    public void delete(@ApiParam(value = "课程id", required = true) @PathVariable(name = "id") String id) {
        this.courseService.delete(id);
    }

    @ApiOperation(value = "更新课程信息")
    @PutMapping(value = "/{id}")
    public CourseDTO update(
            @ApiParam(value = "课程id", required = true) @PathVariable(name = "id") String id,
            @ApiParam(value = "课程信息", required = true) @RequestBody @Validated CourseDTO courseDTO
    ) {
        return this.courseService.update(id, courseDTO);
    }

    @ApiOperation(value = "根据id查找课程类型信息")
    @GetMapping(value = "/{id}")
    public CourseDTO findById(@ApiParam(value = "课程id", required = true) @PathVariable(name = "id") String id) {
        return this.courseService.findById(id);
    }

    @ApiOperation(value = "查找所有课程信息，可选择排序")
    @GetMapping(value = "/all")
    public List<CourseDTO> findAll(
            @ApiParam(value = "排序参数") @RequestParam(value = "param", required = false, defaultValue = "id") String param,
            @ApiParam(value = "排序模式: asc/desc") @RequestParam(value = "pattern", required = false, defaultValue = "asc") String pattern
    ) {
        return this.courseService.findAll(param, pattern);
    }

    @ApiOperation(value = "检查课程编号是否已存在")
    @GetMapping(value = "/checkCodeExist")
    public SimpleDataDTO<Boolean> checkCodeExist(
            @ApiParam(value = "课程详细类型编号", required = true) @RequestParam(value = "code") String code
    ) {
        return this.courseService.checkCodeExist(code);
    }
}